<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" :checked="isAll" @click="handleCheckAll"/>
    </label>
    <span> <span>已完成{{doneTotal}}</span> / 全部{{total}} </span>
    <button class="btn btn-danger" @click="handleClearAllDone">清除已完成任务</button>
  </div>
</template>

<script>
  export default {
    name: "Footer",
    props:['todoList','deleteDoneTodo','checkAllTodo'],
    computed: {
      // 算总数
      total(){
        return this.todoList.length
      },
      // 算完成的总数
      doneTotal(){
        // 第一种写法
        return this.todoList.filter((t)=>{
          return t.done
        }).length

        // 第二种写法
        /* let n = 0
        this.todoList.forEach(t => {
          if(t.done){
            n++
          }
        });
        return n */
      },
      // 算全选那个按钮勾不勾
      isAll(){
        return this.todoList.every((t)=>{
          return t.done
        })
      }
    },
    methods: {
      // 删除所有已完成
      handleClearAllDone(){
        if(confirm('确定删除所有已完成的吗？')){
          this.deleteDoneTodo()
        }
      },
      // 全选
      handleCheckAll(event){
        this.checkAllTodo(event.target.checked)
      }
    },
  };
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>